package com.example.demo.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/*
* 安全用户模型
* */
@Setter
@Getter
@Entity
@Table(name = "user")
@Data
public class User implements Serializable, UserDetails {

    @Column(name = "user_id")
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer userId;

    @Column(name = "username")
    private String username;

    @Column(name = "password")
    private String password;

    @Column(name = "phone")
    private String phone;

    @Column(name="auth")
    private String auth;

    @Column(name="name")
    private String name;

    @Column(name="department")
    private String department;

    @Column(name="sex")
    private String sex;

    @Column(name="age")
    private Integer age;

    @Column(name = "role_id")
    private Integer roleId;


    /**
    * 返回该用户所拥有的权限，在这里即传入用户对应的角色
    *
    **/
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> l=new ArrayList<>();
        l.add(new SimpleGrantedAuthority(this.getAuth()));
        return l;
    }


    @JsonIgnore
    @Override
    public boolean isAccountNonExpired(){
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked(){
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired(){
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled(){
        return true;
    }
}
